package com.lzq.rabbitmqdemo.java_api.topic;

import com.lzq.rabbitmqdemo.java_api.constant.ConnectionConfig;
import com.lzq.rabbitmqdemo.java_api.constant.Constants;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class TopicRabbitProducer {
    public static void main(String[] args) throws Exception{
        ConnectionFactory factory = ConnectionConfig.getFactory();
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            //声明交换机
            channel.exchangeDeclare(Constants.TOPIC_EXCHANGE_NAME, BuiltinExchangeType.TOPIC, true, false, false, null);
            //声明队列
            channel.queueDeclare(Constants.TOPIC_QUEUE_NAME1,true, false, false, null);
            channel.queueDeclare(Constants.TOPIC_QUEUE_NAME2,true, false, false, null);
            //绑定交换机和队列
            channel.queueBind(Constants.TOPIC_QUEUE_NAME1,Constants.TOPIC_EXCHANGE_NAME,"*.error");
            channel.queueBind(Constants.TOPIC_QUEUE_NAME2,Constants.TOPIC_EXCHANGE_NAME,"#.info");
            channel.queueBind(Constants.TOPIC_QUEUE_NAME2,Constants.TOPIC_EXCHANGE_NAME,"*.error");
            String msg = "hello topic, I'm order.error";
            channel.basicPublish(Constants.TOPIC_EXCHANGE_NAME,"order.error",null,msg.getBytes());
            String msg_black = "hello topic, I'm order.pay.info";
            channel.basicPublish(Constants.TOPIC_EXCHANGE_NAME,"order.pay.info",null,msg_black.getBytes());
            String msg_green= "hello topic, I'm pay.error";
            channel.basicPublish(Constants.TOPIC_EXCHANGE_NAME,"pay.error",null,msg_green.getBytes());
        }
    }
}
